class Solution {
    public int romanToInt(String romanStr) {
        int addSum = 0, subSum = 0, preSum = 0, flg = 0, len = romanStr.length();
        for (int i = 0; i < len - 1; i++) {
            int c1 = char2Int(romanStr.charAt(i));
            int c2 = char2Int(romanStr.charAt(i + 1));
            if (c2 == c1) {
                preSum += c1;
            } else if (c1 > c2) {
                addSum += (preSum + c1);
                preSum = 0;
            } else if (c1 < c2) {
                subSum += (preSum + c1);
                preSum = 0;
            }
        }
        addSum = addSum + preSum + char2Int(romanStr.charAt(len - 1));
        return addSum - subSum;
    }

    private int char2Int(char c) {
        switch (c) {
        case 'I':
            return 1;
        case 'V':
            return 5;
        case 'X':
            return 10;
        case 'L':
            return 50;
        case 'C':
            return 100;
        case 'D':
            return 500;
        case 'M':
            return 1000;
        }
        return 0;
    }
}